package scott.pushbox;

import java.awt.AWTEvent;
import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JButton;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.AWTEventListener;
import java.awt.event.KeyEvent;
import java.util.Iterator;

import javax.swing.ImageIcon;

public class PushboxWin extends JFrame {

	private JPanel contentPane;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					PushboxWin frame = new PushboxWin();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public PushboxWin() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 802, 571);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		
		JPanel panel = new JPanel();
		contentPane.add(panel, BorderLayout.CENTER);
		panel.setLayout(new GridLayout(20, 20, 0, 0));
		
		JPanel panel_1 = new JPanel();
		panel_1.setPreferredSize(new Dimension(100, 10));
		contentPane.add(panel_1, BorderLayout.EAST);
		
		JLabel lblNewLabel = new JLabel("推箱子");
		lblNewLabel.setFont(new Font("微软雅黑", Font.BOLD, 20));
		panel_1.add(lblNewLabel);
		
		JButton btnNewButton = new JButton("重新开始");
		btnNewButton.setPreferredSize(new Dimension(85, 23));
		panel_1.add(btnNewButton);
		
		JButton btnNewButton_1 = new JButton("读档");
		btnNewButton_1.setPreferredSize(new Dimension(85, 23));
		panel_1.add(btnNewButton_1);
		
		JButton btnNewButton_1_1 = new JButton("取档");
		btnNewButton_1_1.setPreferredSize(new Dimension(85, 23));
		panel_1.add(btnNewButton_1_1);
		
		//////////////////////////////////////////////
		
		PushBoxGame game = new PushBoxGame();
		game.start();
		int[][] map = game.getMap();
		
		// 添加400个图片到网格中
		JLabel[][] labelMap = new JLabel[20][20];
		
		// 设置图片地图
		game.setLabelMap(labelMap);
		
		for (int y = 0; y < labelMap.length; y++) {
			for (int x = 0; x < labelMap.length; x++) {
				JLabel lblNewLabel_1 = new JLabel("");
				// %s 字符串参数占位符
				String path = "/scott/pushbox/res/imgs/%s.GIF";
				// 替换 %s 为当前坐标值
				path = String.format(path, map[y][x]);
				lblNewLabel_1.setIcon(new ImageIcon(PushboxWin.class.getResource(path)));
				panel.add(lblNewLabel_1);
				labelMap[y][x] = lblNewLabel_1;
			}	
		}
		
		// 设计时, 可以不要执行该方法
		// 自动调整窗口为最适大小
		this.pack();
		
		
		// 添加全局的键盘监听器
		Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
			// AWTEvent 通用的事件对象
		    public void eventDispatched(AWTEvent event) {
		    	// KeyEvent 键盘事件对象
		        if (((KeyEvent) event).getID() == KeyEvent.KEY_PRESSED) {
		        	System.out.println("--------------------" + event);
		        	if(((KeyEvent) event).getKeyCode() == KeyEvent.VK_UP) {
		        		System.out.println("-------------------- 上");
		        		game.up();
		        	} else if(((KeyEvent) event).getKeyCode() == KeyEvent.VK_DOWN) {
		        		game.down();
		        	} else if(((KeyEvent) event).getKeyCode() == KeyEvent.VK_LEFT) {
		        		game.left();
		        	} else if(((KeyEvent) event).getKeyCode() == KeyEvent.VK_RIGHT) {
		        		game.right();
		        	}
		        }
		    }
		}, AWTEvent.KEY_EVENT_MASK);
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
	}

}
